package com.szosen.jkzj.activity.ggws;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;

import com.szosen.database.ChineseMedicineManage;
import com.szosen.database.DBService;
import com.szosen.jkzj.R;
import com.szosen.jkzj.activity.BaseActivity;
import com.szosen.json.ZhongYiWenJuan;
import com.szosen.json.database.ArchiveBean;
import com.szosen.util.TimeHelper;
import com.szosen.util.ToastTool;
import com.szosen.util.ZYYBitmapUtil;
import com.szosen.web.WebService;
import com.szosen.xml.XmlTool;

/***
 * 老年人中医药管理
 * 
 * @author geshenjibi
 * 
 */
public class Lnrzyygl extends BaseActivity {
	private EditText et_name, et_serial_id, et_date, et_doctorName;
	private Button btn_save;

	// 体质类型
	RadioGroup ZYQXZ1; // 中医气虚质代码
	RadioButton ZYQXZ1_1;
	RadioButton ZYQXZ1_2;
	EditText ZYQXZ1_2_et_other;
	RadioGroup ZYYXZ1; // 中医阳虚质代码
	RadioButton ZYYXZ1_1;
	RadioButton ZYYXZ1_2;
	EditText ZYYXZ1_2_et_other;
	RadioGroup ZYYXZ2; // 中医阴虚质代码
	RadioButton ZYYXZ2_1;
	RadioButton ZYYXZ2_2;
	EditText ZYYXZ2_2_et_other;
	RadioGroup ZYTXZ1; // 中医痰湿质代码
	RadioButton ZYTXZ1_1;
	RadioButton ZYTXZ1_2;
	EditText ZYTXZ1_2_et_other;
	RadioGroup ZYXRZ1; // 中医湿热质代码
	RadioButton ZYXRZ1_1;
	RadioButton ZYXRZ1_2;
	EditText ZYXRZ1_2_et_other;
	RadioGroup ZYXYZ1; // 中医血瘀质代码
	RadioButton ZYXYZ1_1;
	RadioButton ZYXYZ1_2;
	EditText ZYXYZ1_2_et_other;
	RadioGroup ZYQYZ1; // 中医气郁质代码
	RadioButton ZYQYZ1_1;
	RadioButton ZYQYZ1_2;
	EditText ZYQYZ1_2_et_other;
	RadioGroup ZYTBZ1; // 中医特秉质代码
	RadioButton ZYTBZ1_1;
	RadioButton ZYTBZ1_2;
	EditText ZYTBZ1_2_et_other;
	RadioGroup ZYPHZ1; // 中医平和质代码
	RadioButton ZYPHZ1_1;
	RadioButton ZYPHZ1_2;
	EditText ZYPHZ1_2_et_other;

	EditText ZYQXZ1_2_et_defen;
	EditText ZYYXZ1_2_et_defen;
	EditText ZYYXZ2_2_et_defen;
	EditText ZYTXZ1_2_et_defen;
	EditText ZYXRZ1_2_et_defen;
	EditText ZYXYZ1_2_et_defen;
	EditText ZYQYZ1_2_et_defen;
	EditText ZYTBZ1_2_et_defen;
	EditText ZYPHZ1_2_et_defen;
	int qxz_defen = 0;
	int yyz1_defen = 0;
	int yyz2_defen = 0;
	int txz_defen = 0;
	int srz_defen = 0;
	int xyz_defen = 0;
	int qyz_defen = 0;
	int tbz_defen = 0;
	int phz_defen = 0;
	private String time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ggws_lnrzyygl_activity);
		super.setTitleContent("【老年人中医药管理】");
		initView();
	}

	private void setAllChoiceEditText() {
		String[] zyybjzds = new String[] { "情志调摄", "饮食调养", "起居调摄", "运动保健",
				"穴位保健" };
		setChoiceEditText(R.id.ZYPHZ1_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYQXZ1_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYYXZ1_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYYXZ2_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYTXZ1_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYXRZ1_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYXYZ1_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYQYZ1_2_et, zyybjzds, null);
		setChoiceEditText(R.id.ZYTBZ1_2_et, zyybjzds, null);
	}

	/***
	 * 切割中医指导的6个东西，改为多选然后
	 */
	private String getUploadStr_zyzd(String text, EditText et_other) {
		String result = "";
		StringBuilder sb = new StringBuilder();
		if (!TextUtils.isEmpty(text)) {
			String[] datas = text.split("\t");
			for (int i = 0; i < datas.length; i++) {
				switch (datas[i]) {
				case "情志调摄":
					sb.append("1,");
					break;
				case "饮食调养":
					sb.append("2,");
					break;
				case "起居调摄":
					sb.append("3,");
					break;
				case "运动保健":
					sb.append("4,");
					break;
				case "穴位保健":
					sb.append("5,");
					break;
				default:
					break;
				}
			}
		}
		if (!TextUtils.isEmpty(et_other.getText().toString())) {
			sb.append("6,");
		}
		result = sb.toString();
		return result;
	}

	/***
	 * 将数据库的1,2,3变为情志调摄、饮食调养、起居调摄
	 */
	private String getSetDataFromDB(String text) {
		String result = "";
		StringBuilder sb = new StringBuilder();
		if (!TextUtils.isEmpty(text)) {
			String[] datas = text.split(",");
			for (int i = 0; i < datas.length; i++) {
				switch (datas[i]) {
				case "1":
					sb.append("情志调摄\t");
					break;
				case "2":
					sb.append("饮食调养\t");
					break;
				case "3":
					sb.append("起居调摄\t");
					break;
				case "4":
					sb.append("运动保健\t");
					break;
				case "5":
					sb.append("穴位保健\t");
					break;
				default:
					break;
				}
			}
		}
		result = sb.toString();
		return result;
	}

	private void initView() {
		time = getIntent().getStringExtra("time");
		et_name = (EditText) findViewById(R.id.patient_name);
		et_serial_id = (EditText) findViewById(R.id.serial_id);
		et_date = (EditText) findViewById(R.id.oldpeopleservice_det_reportdate);
		if (TextUtils.isEmpty(time)) {
			time = TimeHelper.getMainTimeKey();
			et_date.setText(TimeHelper.getJKTJCheckDate());
		}
		et_doctorName = (EditText) findViewById(R.id.oldpeopleservice_reportdoctor);
		btn_save = (Button) findViewById(R.id.oldpeopleservice_save);
		et_name.setText(getHealthRecord().getPersonName());
		Button btn_jisuanScore = (Button) findViewById(R.id.oldpeopleservice_jisuanScore);
		btn_jisuanScore.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				jisuanScore();
			}
		});
		btn_save.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				jisuanScore();
				upload();
			}
		});

		Activity view = this;
		ZYPHZ1 = (RadioGroup) view.findViewById(R.id.ZYPHZ1);
		ZYPHZ1_1 = (RadioButton) view.findViewById(R.id.ZYPHZ1_1);
		ZYPHZ1_2 = (RadioButton) view.findViewById(R.id.ZYPHZ1_2);
		ZYPHZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYPHZ1_2_et_other);
		ZYQXZ1 = (RadioGroup) view.findViewById(R.id.ZYQXZ1);
		ZYQXZ1_1 = (RadioButton) view.findViewById(R.id.ZYQXZ1_1);
		ZYQXZ1_2 = (RadioButton) view.findViewById(R.id.ZYQXZ1_2);
		ZYQXZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYQXZ1_2_et_other);
		ZYYXZ1 = (RadioGroup) view.findViewById(R.id.ZYYXZ1);
		ZYYXZ1_1 = (RadioButton) view.findViewById(R.id.ZYYXZ1_1);
		ZYYXZ1_2 = (RadioButton) view.findViewById(R.id.ZYYXZ1_2);
		ZYYXZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYYXZ1_2_et_other);
		ZYYXZ2 = (RadioGroup) view.findViewById(R.id.ZYYXZ2);
		ZYYXZ2_1 = (RadioButton) view.findViewById(R.id.ZYYXZ2_1);
		ZYYXZ2_2 = (RadioButton) view.findViewById(R.id.ZYYXZ2_2);
		ZYYXZ2_2_et_other = (EditText) view
				.findViewById(R.id.ZYYXZ2_2_et_other);
		ZYTXZ1 = (RadioGroup) view.findViewById(R.id.ZYTXZ1);
		ZYTXZ1_1 = (RadioButton) view.findViewById(R.id.ZYTXZ1_1);
		ZYTXZ1_2 = (RadioButton) view.findViewById(R.id.ZYTXZ1_2);
		ZYTXZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYTXZ1_2_et_other);
		ZYXRZ1 = (RadioGroup) view.findViewById(R.id.ZYXRZ1);
		ZYXRZ1_1 = (RadioButton) view.findViewById(R.id.ZYXRZ1_1);
		ZYXRZ1_2 = (RadioButton) view.findViewById(R.id.ZYXRZ1_2);
		ZYXRZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYXRZ1_2_et_other);
		ZYXYZ1 = (RadioGroup) view.findViewById(R.id.ZYXYZ1);
		ZYXYZ1_1 = (RadioButton) view.findViewById(R.id.ZYXYZ1_1);
		ZYXYZ1_2 = (RadioButton) view.findViewById(R.id.ZYXYZ1_2);
		ZYXYZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYXYZ1_2_et_other);
		ZYQYZ1 = (RadioGroup) view.findViewById(R.id.ZYQYZ1);
		ZYQYZ1_1 = (RadioButton) view.findViewById(R.id.ZYQYZ1_1);
		ZYQYZ1_2 = (RadioButton) view.findViewById(R.id.ZYQYZ1_2);
		ZYQYZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYQYZ1_2_et_other);
		ZYTBZ1 = (RadioGroup) view.findViewById(R.id.ZYTBZ1);
		ZYTBZ1_1 = (RadioButton) view.findViewById(R.id.ZYTBZ1_1);
		ZYTBZ1_2 = (RadioButton) view.findViewById(R.id.ZYTBZ1_2);
		ZYTBZ1_2_et_other = (EditText) view
				.findViewById(R.id.ZYTBZ1_2_et_other);
		setAllChoiceEditText();

		ZYQXZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYQXZ1_2_et_defen);
		ZYYXZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYYXZ1_2_et_defen);
		ZYYXZ2_2_et_defen = (EditText) view
				.findViewById(R.id.ZYYXZ2_2_et_defen);
		ZYTXZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYTXZ1_2_et_defen);
		ZYXRZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYXRZ1_2_et_defen);
		ZYXYZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYXYZ1_2_et_defen);
		ZYQYZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYQYZ1_2_et_defen);
		ZYTBZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYTBZ1_2_et_defen);
		ZYPHZ1_2_et_defen = (EditText) view
				.findViewById(R.id.ZYPHZ1_2_et_defen);
		RatingBar ratingBar33 = (RatingBar) findViewById(R.id.ratingbar33);
		ratingBar33
				.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

					@Override
					public void onRatingChanged(RatingBar ratingBar,
							float rating, boolean fromUser) {
						jisuanScore();
					}
				});

	}

	private void jisuanScore() {

		int defen1 = getRatingbarInt(R.id.ratingbar1);
		int defen2 = getRatingbarInt(R.id.ratingbar2);
		int defen3 = getRatingbarInt(R.id.ratingbar3);
		int defen4 = getRatingbarInt(R.id.ratingbar4);
		int defen5 = getRatingbarInt(R.id.ratingbar5);
		int defen6 = getRatingbarInt(R.id.ratingbar6);
		int defen7 = getRatingbarInt(R.id.ratingbar7);
		int defen8 = getRatingbarInt(R.id.ratingbar8);
		int defen9 = getRatingbarInt(R.id.ratingbar9);
		int defen10 = getRatingbarInt(R.id.ratingbar10);
		int defen11 = getRatingbarInt(R.id.ratingbar11);
		int defen12 = getRatingbarInt(R.id.ratingbar12);
		int defen13 = getRatingbarInt(R.id.ratingbar13);
		int defen14 = getRatingbarInt(R.id.ratingbar14);
		int defen15 = getRatingbarInt(R.id.ratingbar15);
		int defen16 = getRatingbarInt(R.id.ratingbar16);
		int defen17 = getRatingbarInt(R.id.ratingbar17);
		int defen18 = getRatingbarInt(R.id.ratingbar18);
		int defen19 = getRatingbarInt(R.id.ratingbar19);
		int defen20 = getRatingbarInt(R.id.ratingbar20);
		int defen21 = getRatingbarInt(R.id.ratingbar21);
		int defen22 = getRatingbarInt(R.id.ratingbar22);
		int defen23 = getRatingbarInt(R.id.ratingbar23);
		int defen24 = getRatingbarInt(R.id.ratingbar24);
		int defen25 = getRatingbarInt(R.id.ratingbar25);
		int defen26 = getRatingbarInt(R.id.ratingbar26);
		int defen27 = getRatingbarInt(R.id.ratingbar27);
		int defen28 = getRatingbarInt(R.id.ratingbar28);
		int defen29 = getRatingbarInt(R.id.ratingbar29);
		int defen30 = getRatingbarInt(R.id.ratingbar30);
		int defen31 = getRatingbarInt(R.id.ratingbar31);
		int defen32 = getRatingbarInt(R.id.ratingbar32);
		int defen33 = getRatingbarInt(R.id.ratingbar33);
		// 气虚质（2）（3）（4）（14）
		// 阳虚质（11）（12）（13）（29）
		// 阴虚质（10）（21）（26）（31）
		// 痰湿质（9）（16）（28）（32）
		// 湿热质（23）（25）（27）（30）
		// 血瘀质（19）（22）（24）（33）
		// 气郁质（5）（6）（7）（8）
		// 特禀质（15）（17）（18）（20）
		// 平和质（1）（2）（4）（5）（13）
		// （其中，（2）（4）（5）（13）反向计分，即1→5，2→4，3→3，4→2，5→1）
		qxz_defen = defen2 + defen3 + defen4 + defen14;
		yyz1_defen = defen11 + defen12 + defen13 + defen29;
		yyz2_defen = defen10 + defen21 + defen26 + defen31;
		txz_defen = defen9 + defen16 + defen28 + defen32;
		srz_defen = defen23 + defen25 + defen27 + defen30;
		xyz_defen = defen19 + defen22 + defen24 + defen33;
		qyz_defen = defen5 + defen6 + defen7 + defen8;
		tbz_defen = defen15 + defen17 + defen18 + defen20;
		phz_defen = 0;
		if (isTizhi(qxz_defen)) {
			ZYQXZ1_2.setChecked(true);
		} else if (isTizhiQingxiangshi(qxz_defen)) {
			ZYQXZ1_1.setChecked(true);
		} else {
			ZYQXZ1_1.setChecked(false);
			ZYQXZ1_2.setChecked(false);
		}
		if (isTizhi(yyz1_defen)) {
			ZYYXZ1_2.setChecked(true);
		} else if (isTizhiQingxiangshi(yyz1_defen)) {
			ZYYXZ1_1.setChecked(true);
		} else {
			ZYYXZ1_1.setChecked(false);
			ZYYXZ1_2.setChecked(false);
		}
		if (isTizhi(yyz2_defen)) {
			ZYYXZ2_2.setChecked(true);
		} else if (isTizhiQingxiangshi(yyz2_defen)) {
			ZYYXZ2_1.setChecked(true);
		} else {
			ZYYXZ2_1.setChecked(false);
			ZYYXZ2_2.setChecked(false);
		}
		if (isTizhi(txz_defen)) {
			ZYTXZ1_2.setChecked(true);
		} else if (isTizhiQingxiangshi(txz_defen)) {
			ZYTXZ1_1.setChecked(true);
		} else {
			ZYTXZ1_1.setChecked(false);
			ZYTXZ1_2.setChecked(false);
		}
		if (isTizhi(srz_defen)) {
			ZYXRZ1_2.setChecked(true);
		} else if (isTizhiQingxiangshi(srz_defen)) {
			ZYXRZ1_1.setChecked(true);
		} else {
			ZYXRZ1_1.setChecked(false);
			ZYXRZ1_2.setChecked(false);
		}
		if (isTizhi(xyz_defen)) {
			ZYXYZ1_2.setChecked(true);
		} else if (isTizhiQingxiangshi(xyz_defen)) {
			ZYXYZ1_1.setChecked(true);
		} else {
			ZYXYZ1_1.setChecked(false);
			ZYXYZ1_2.setChecked(false);
		}
		if (isTizhi(qyz_defen)) {
			ZYQYZ1_2.setChecked(true);
		} else if (isTizhiQingxiangshi(qyz_defen)) {
			ZYQYZ1_1.setChecked(true);
		} else {
			ZYQYZ1_1.setChecked(false);
			ZYQYZ1_2.setChecked(false);
		}
		if (isTizhi(tbz_defen)) {
			ZYTBZ1_2.setChecked(true);
		} else if (isTizhiQingxiangshi(tbz_defen)) {
			ZYTBZ1_1.setChecked(true);
		} else {
			ZYTBZ1_1.setChecked(false);
			ZYTBZ1_2.setChecked(false);
		}
		// if (isTizhi(phz_defen)) {
		// ZYPHZ1_2.setChecked(true);
		// } else if(isTizhiQingxiangshi(qxz_defen)){
		// ZYPHZ1_1.setChecked(true);
		// }

		if (defen2 > 0 && defen4 > 0 && defen5 > 0 && defen13 > 0) {
			phz_defen = defen1 + (6 - defen2) + (6 - defen4) + (6 - defen5)
					+ (6 - defen13);
		}
		// int zong = qxz_defen + yyz1_defen + yyz2_defen + txz_defen +
		// srz_defen
		// + xyz_defen + qyz_defen + tbz_defen;
		int zong = phz_defen;
		if ((qxz_defen < 8) && (yyz1_defen < 8) && (yyz2_defen < 8)
				&& (txz_defen < 8) && (srz_defen < 8) && (xyz_defen < 8)
				&& (qyz_defen < 8) && (tbz_defen < 8)) {
			if (zong >= 17) {
				ZYPHZ1_2.setChecked(true);
			}
		} else if ((qxz_defen < 10) && (yyz1_defen < 10) && (yyz2_defen < 10)
				&& (txz_defen < 10) && (srz_defen < 10) && (xyz_defen < 10)
				&& (qyz_defen < 10) && (tbz_defen < 10)) {
			if (zong >= 17) {
				ZYPHZ1_1.setChecked(true);
			}
		} else {
			ZYPHZ1_1.setChecked(false);
			ZYPHZ1_2.setChecked(false);
		}
		ZYQXZ1_2_et_defen.setText("" + qxz_defen);
		ZYYXZ1_2_et_defen.setText("" + yyz1_defen);
		ZYYXZ2_2_et_defen.setText("" + yyz2_defen);
		ZYTXZ1_2_et_defen.setText("" + txz_defen);
		ZYXRZ1_2_et_defen.setText("" + srz_defen);
		ZYXYZ1_2_et_defen.setText("" + xyz_defen);
		ZYQYZ1_2_et_defen.setText("" + qyz_defen);
		ZYTBZ1_2_et_defen.setText("" + tbz_defen);
		ZYPHZ1_2_et_defen.setText("" + phz_defen);

	}

	private boolean isTizhi(int count) {
		if (count >= 11) {
			return true;
		} else {
			return false;
		}
	}

	/***
	 * 判断是不是倾向是的体质。
	 * 
	 * @param count
	 * @return
	 */
	private boolean isTizhiQingxiangshi(int count) {
		if (count == 9 || count == 10) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onResume() {
		super.onResume();
		getDataFromDB();
	}

	/***
	 * 获取星星评分的分数
	 */
	private String getRatingbarStr(int RatingbarId) {
		RatingBar ratingBar = (RatingBar) findViewById(RatingbarId);
		int result = (int) ratingBar.getRating();
		return result + "";
	}

	/***
	 * 获取星星评分的分数
	 */
	private int getRatingbarInt(int RatingbarId) {
		RatingBar ratingBar = (RatingBar) findViewById(RatingbarId);
		int result = (int) ratingBar.getRating();
		return result;
	}

	/***
	 * 给ratingBar设置数值。
	 * 
	 * @param ratingBarId
	 * @param value
	 */
	private void setRatingBarValue(int ratingBarId, String value) {
		try {
			if (TextUtils.isEmpty(value)) {
				return;
			}
			int i = Integer.valueOf(value);
			if (i >= 0 && i <= 5) {
				RatingBar ratingBar = (RatingBar) findViewById(ratingBarId);
				ratingBar.setRating(i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void getDataFromDB() {
		ChineseMedicineManage queryObj = new ChineseMedicineManage();
		queryObj.setTime(time);
		List<Object> lists = (List<Object>) DBService.queryByKey(queryObj);
		ChineseMedicineManage manage;
		if (lists.size() > 0) {
			manage = (ChineseMedicineManage) lists.get(0);
		} else {
			return;
		}
		et_date.setText(manage.getReportDate());
		setRatingBarValue(R.id.ratingbar1, manage.getEnergyFull());
		setRatingBarValue(R.id.ratingbar2, manage.getEasyWeary());
		setRatingBarValue(R.id.ratingbar3, manage.getEasyPant());
		setRatingBarValue(R.id.ratingbar4, manage.getVoiceWeak());
		setRatingBarValue(R.id.ratingbar5, manage.getMoodiness());
		setRatingBarValue(R.id.ratingbar6, manage.getNervous());
		setRatingBarValue(R.id.ratingbar7, manage.getLoneliness());
		setRatingBarValue(R.id.ratingbar8, manage.getEasyScare());
		setRatingBarValue(R.id.ratingbar9, manage.getOverweight());
		setRatingBarValue(R.id.ratingbar10, manage.getEyeDry());
		setRatingBarValue(R.id.ratingbar11, manage.getFootFearCold());
		setRatingBarValue(R.id.ratingbar12, manage.getBackFearCold());
		setRatingBarValue(R.id.ratingbar13, manage.getFearCold());
		setRatingBarValue(R.id.ratingbar14, manage.getCold());
		setRatingBarValue(R.id.ratingbar15, manage.getRhinobyon());
		setRatingBarValue(R.id.ratingbar16, manage.getMouthGreasy());
		setRatingBarValue(R.id.ratingbar17, manage.getAllergy());
		setRatingBarValue(R.id.ratingbar18, manage.getSkinUrticaria());
		setRatingBarValue(R.id.ratingbar19, manage.getSkinBleeding());
		setRatingBarValue(R.id.ratingbar20, manage.getSkinRed());
		setRatingBarValue(R.id.ratingbar21, manage.getSkinDry());
		setRatingBarValue(R.id.ratingbar22, manage.getLimbsNumb());
		setRatingBarValue(R.id.ratingbar23, manage.getFaceGreasy());
		setRatingBarValue(R.id.ratingbar24, manage.getFaceDim());
		setRatingBarValue(R.id.ratingbar25, manage.getSkinEczema());
		setRatingBarValue(R.id.ratingbar26, manage.getMouthDry());
		setRatingBarValue(R.id.ratingbar27, manage.getBitterTaste());
		setRatingBarValue(R.id.ratingbar28, manage.getBellyLarge());
		setRatingBarValue(R.id.ratingbar29, manage.getFearCool());
		setRatingBarValue(R.id.ratingbar30, manage.getStoolStiction());
		setRatingBarValue(R.id.ratingbar31, manage.getStoolDry());
		setRatingBarValue(R.id.ratingbar32, manage.getFurStodgily());
		setRatingBarValue(R.id.ratingbar33, manage.getStasisPurple());// 问题33
		//
		setText(R.id.ZYQXZ1_2_et, getSetDataFromDB(manage.getHealthGuide1()));
		setText(R.id.ZYYXZ1_2_et, getSetDataFromDB(manage.getHealthGuide2()));
		setText(R.id.ZYYXZ2_2_et, getSetDataFromDB(manage.getHealthGuide3()));
		setText(R.id.ZYTXZ1_2_et, getSetDataFromDB(manage.getHealthGuide4()));
		setText(R.id.ZYXRZ1_2_et, getSetDataFromDB(manage.getHealthGuide5()));
		setText(R.id.ZYXYZ1_2_et, getSetDataFromDB(manage.getHealthGuide6()));
		setText(R.id.ZYQYZ1_2_et, getSetDataFromDB(manage.getHealthGuide7()));
		setText(R.id.ZYTBZ1_2_et, getSetDataFromDB(manage.getHealthGuide8()));
		setText(R.id.ZYPHZ1_2_et, getSetDataFromDB(manage.getHealthGuide9()));
		String ph1 = manage.getPhysiqueIdentify1();
		String ph2 = manage.getPhysiqueIdentify2();
		String ph3 = manage.getPhysiqueIdentify3();
		String ph4 = manage.getPhysiqueIdentify4();
		String ph5 = manage.getPhysiqueIdentify5();
		String ph6 = manage.getPhysiqueIdentify6();
		String ph7 = manage.getPhysiqueIdentify7();
		String ph8 = manage.getPhysiqueIdentify8();
		String ph9 = manage.getPhysiqueIdentify9();
		if ("2".equals(ph1)) {
			ZYQXZ1_1.setChecked(true);
		} else if ("1".equals(ph1)) {
			ZYQXZ1_2.setChecked(true);
		}
		if ("2".equals(ph2)) {
			ZYYXZ1_1.setChecked(true);
		} else if ("1".equals(ph2)) {
			ZYYXZ1_2.setChecked(true);
		}
		if ("2".equals(ph3)) {
			ZYYXZ2_1.setChecked(true);
		} else if ("1".equals(ph3)) {
			ZYYXZ2_2.setChecked(true);
		}
		if ("2".equals(ph4)) {
			ZYTXZ1_1.setChecked(true);
		} else if ("1".equals(ph4)) {
			ZYTXZ1_2.setChecked(true);
		}
		if ("2".equals(ph5)) {
			ZYXRZ1_1.setChecked(true);
		} else if ("1".equals(ph5)) {
			ZYXRZ1_2.setChecked(true);
		}
		if ("2".equals(ph6)) {
			ZYXYZ1_1.setChecked(true);
		} else if ("1".equals(ph6)) {
			ZYXYZ1_2.setChecked(true);
		}
		if ("2".equals(ph7)) {
			ZYQYZ1_1.setChecked(true);
		} else if ("1".equals(ph7)) {
			ZYQYZ1_2.setChecked(true);
		}
		if ("2".equals(ph8)) {
			ZYTBZ1_1.setChecked(true);
		} else if ("1".equals(ph8)) {
			ZYTBZ1_2.setChecked(true);
		}
		if ("2".equals(ph9)) {
			ZYPHZ1_1.setChecked(true);
		} else if ("1".equals(ph9)) {
			ZYPHZ1_2.setChecked(true);
		}
		ZYQXZ1_2_et_other.setText(manage.getOther1());
		ZYYXZ1_2_et_other.setText(manage.getOther2());
		ZYYXZ2_2_et_other.setText(manage.getOther3());
		ZYTXZ1_2_et_other.setText(manage.getOther4());
		ZYXRZ1_2_et_other.setText(manage.getOther5());
		ZYXYZ1_2_et_other.setText(manage.getOther6());
		ZYQYZ1_2_et_other.setText(manage.getOther7());
		ZYTBZ1_2_et_other.setText(manage.getOther8());
		ZYPHZ1_2_et_other.setText(manage.getOther9());
		ZYQXZ1_2_et_defen.setText(manage.getScore1());
		ZYYXZ1_2_et_defen.setText(manage.getScore2());
		ZYYXZ2_2_et_defen.setText(manage.getScore3());
		ZYTXZ1_2_et_defen.setText(manage.getScore4());
		ZYXRZ1_2_et_defen.setText(manage.getScore5());
		ZYXYZ1_2_et_defen.setText(manage.getScore6());
		ZYQYZ1_2_et_defen.setText(manage.getScore7());
		ZYTBZ1_2_et_defen.setText(manage.getScore8());
		ZYPHZ1_2_et_defen.setText(manage.getScore9());

	}

	/***
	 * 获取页面的manager的值。
	 */
	private ChineseMedicineManage getManagerByUI() {
		final ChineseMedicineManage manage = new ChineseMedicineManage();
		manage.setEnergyFull(getRatingbarStr(R.id.ratingbar1));// 问题1</energyFull;//
		manage.setEasyWeary(getRatingbarStr(R.id.ratingbar2));// 问题2</easyWeary;//
		manage.setEasyPant(getRatingbarStr(R.id.ratingbar3));// 问题3</easyPant;//
		manage.setVoiceWeak(getRatingbarStr(R.id.ratingbar4));// 问题4</voiceWeak;//
		manage.setMoodiness(getRatingbarStr(R.id.ratingbar5));// 问题5</moodiness;//
		manage.setNervous(getRatingbarStr(R.id.ratingbar6));// 问题6manage.set/nervous;//
		manage.setLoneliness(getRatingbarStr(R.id.ratingbar7));// 问题7manage.set/loneliness;//
		manage.setEasyScare(getRatingbarStr(R.id.ratingbar8));// 问题8manage.set/easyScare;//
		manage.setOverweight(getRatingbarStr(R.id.ratingbar9));// 问题9manage.set/overweight;//
		manage.setEyeDry(getRatingbarStr(R.id.ratingbar10));// 问题10manage.set/eyeDry;//
		manage.setFootFearCold(getRatingbarStr(R.id.ratingbar11));// 问题11manage.set/footFearCold;//
		manage.setBackFearCold(getRatingbarStr(R.id.ratingbar12));// 问题12manage.set/backFearCold;//
		manage.setFearCold(getRatingbarStr(R.id.ratingbar13));// 问题13manage.set/fearCold;//
		manage.setCold(getRatingbarStr(R.id.ratingbar14));// 问题14manage.set/cold;//
		manage.setRhinobyon(getRatingbarStr(R.id.ratingbar15));// 问题15manage.set/rhinobyon;//
		manage.setMouthGreasy(getRatingbarStr(R.id.ratingbar16));// 问题16manage.set/mouthGreasy;//
		manage.setAllergy(getRatingbarStr(R.id.ratingbar17));// 问题17manage.set/allergy;//
		manage.setSkinUrticaria(getRatingbarStr(R.id.ratingbar18));// 问题18manage.set/skinUrticaria;//
		manage.setSkinBleeding(getRatingbarStr(R.id.ratingbar19));// 问题19manage.set/skinBleeding;//
		manage.setSkinRed(getRatingbarStr(R.id.ratingbar20));// 问题20manage.set/skinRed;//
		manage.setSkinDry(getRatingbarStr(R.id.ratingbar21));// 问题21manage.set/skinDry;//
		manage.setLimbsNumb(getRatingbarStr(R.id.ratingbar22));// 问题22manage.set/limbsNumb;//
		manage.setFaceGreasy(getRatingbarStr(R.id.ratingbar23));// 问题23manage.set/faceGreasy;//
		manage.setFaceDim(getRatingbarStr(R.id.ratingbar24));// 问题24manage.set/faceDim;//
		manage.setSkinEczema(getRatingbarStr(R.id.ratingbar25));// 问题25manage.set/skinEczema;//
		manage.setMouthDry(getRatingbarStr(R.id.ratingbar26));// 问题26manage.set/mouthDry;//
		manage.setBitterTaste(getRatingbarStr(R.id.ratingbar27));// 问题27manage.set/bitterTaste;//
		manage.setBellyLarge(getRatingbarStr(R.id.ratingbar28));// 问题28manage.set/bellyLarge;//
		manage.setFearCool(getRatingbarStr(R.id.ratingbar29));// 问题29manage.set/fearCool;//
		manage.setStoolStiction(getRatingbarStr(R.id.ratingbar30));// 问题30manage.set/stoolStiction;//
		manage.setStoolDry(getRatingbarStr(R.id.ratingbar31));// 问题31manage.set/stoolDry;//
		manage.setFurStodgily(getRatingbarStr(R.id.ratingbar32));// 问题32manage.set/furStodgily;//
		manage.setStasisPurple(getRatingbarStr(R.id.ratingbar33));// 问题33</stasisPurple;//
		manage.setTime(time);
		manage.setReportDate(et_date.getText().toString());
		manage.setIdCard(BaseActivity.getIdCard());
		manage.setCheckid(time);
		manage.setHealthGuide1(getUploadStr_zyzd(getTextById(R.id.ZYQXZ1_2_et),
				ZYQXZ1_2_et_other));
		manage.setHealthGuide2(getUploadStr_zyzd(getTextById(R.id.ZYYXZ1_2_et),
				ZYYXZ1_2_et_other));
		manage.setHealthGuide3(getUploadStr_zyzd(getTextById(R.id.ZYYXZ2_2_et),
				ZYYXZ2_2_et_other));
		manage.setHealthGuide4(getUploadStr_zyzd(getTextById(R.id.ZYTXZ1_2_et),
				ZYTXZ1_2_et_other));
		manage.setHealthGuide5(getUploadStr_zyzd(getTextById(R.id.ZYXRZ1_2_et),
				ZYXRZ1_2_et_other));
		manage.setHealthGuide6(getUploadStr_zyzd(getTextById(R.id.ZYXYZ1_2_et),
				ZYXYZ1_2_et_other));
		manage.setHealthGuide7(getUploadStr_zyzd(getTextById(R.id.ZYQYZ1_2_et),
				ZYQYZ1_2_et_other));
		manage.setHealthGuide8(getUploadStr_zyzd(getTextById(R.id.ZYTBZ1_2_et),
				ZYTBZ1_2_et_other));
		manage.setHealthGuide9(getUploadStr_zyzd(getTextById(R.id.ZYPHZ1_2_et),
				ZYPHZ1_2_et_other));
		if (ZYQXZ1_1.isChecked()) {
			manage.setPhysiqueIdentify1("2");
		} else if (ZYQXZ1_2.isChecked()) {
			manage.setPhysiqueIdentify1("1");
		} else {
			manage.setPhysiqueIdentify1("");
		}
		if (ZYYXZ1_1.isChecked()) {
			manage.setPhysiqueIdentify2("2");
		} else if (ZYYXZ1_2.isChecked()) {
			manage.setPhysiqueIdentify2("1");
		}
		if (ZYYXZ2_1.isChecked()) {
			manage.setPhysiqueIdentify3("2");
		} else if (ZYYXZ2_2.isChecked()) {
			manage.setPhysiqueIdentify3("1");
		}

		if (ZYTXZ1_1.isChecked()) {
			manage.setPhysiqueIdentify4("2");
		} else if (ZYTXZ1_2.isChecked()) {
			manage.setPhysiqueIdentify4("1");
		}

		if (ZYXRZ1_1.isChecked()) {
			manage.setPhysiqueIdentify5("2");
		} else if (ZYXRZ1_2.isChecked()) {
			manage.setPhysiqueIdentify5("1");
		}

		if (ZYXYZ1_1.isChecked()) {
			manage.setPhysiqueIdentify6("2");
		} else if (ZYXYZ1_2.isChecked()) {
			manage.setPhysiqueIdentify6("1");
		}
		if (ZYQYZ1_1.isChecked()) {
			manage.setPhysiqueIdentify7("2");
		} else if (ZYQYZ1_2.isChecked()) {
			manage.setPhysiqueIdentify7("1");
		}
		if (ZYTBZ1_1.isChecked()) {
			manage.setPhysiqueIdentify8("2");
		} else if (ZYTBZ1_2.isChecked()) {
			manage.setPhysiqueIdentify8("1");
		}
		if (ZYPHZ1_1.isChecked()) {
			manage.setPhysiqueIdentify9("2");
		} else if (ZYPHZ1_2.isChecked()) {
			manage.setPhysiqueIdentify9("1");
		}
		manage.setOther1(getTextByEt(ZYQXZ1_2_et_other));
		manage.setOther2(getTextByEt(ZYYXZ1_2_et_other));
		manage.setOther3(getTextByEt(ZYYXZ2_2_et_other));
		manage.setOther4(getTextByEt(ZYTXZ1_2_et_other));
		manage.setOther5(getTextByEt(ZYXRZ1_2_et_other));
		manage.setOther6(getTextByEt(ZYXYZ1_2_et_other));
		manage.setOther7(getTextByEt(ZYQYZ1_2_et_other));
		manage.setOther8(getTextByEt(ZYTBZ1_2_et_other));
		manage.setOther9(getTextByEt(ZYPHZ1_2_et_other));
		manage.setScore1(ZYQXZ1_2_et_defen.getText().toString());
		manage.setScore2(ZYYXZ1_2_et_defen.getText().toString());
		manage.setScore3(ZYYXZ2_2_et_defen.getText().toString());
		manage.setScore4(ZYTXZ1_2_et_defen.getText().toString());
		manage.setScore5(ZYXRZ1_2_et_defen.getText().toString());
		manage.setScore6(ZYXYZ1_2_et_defen.getText().toString());
		manage.setScore7(ZYQYZ1_2_et_defen.getText().toString());
		manage.setScore8(ZYTBZ1_2_et_defen.getText().toString());
		manage.setScore9(ZYPHZ1_2_et_defen.getText().toString());
		return manage;
	}

	public void printTable(View view) {
		final ChineseMedicineManage manage = getManagerByUI();
		ZYYBitmapUtil.createZhongyitizhiBitmap(context, manage, BaseActivity.getName(), null);
	}

	public void upload() {
		if (!isEditTextOK()) {
			return;
		}
		final ChineseMedicineManage manage = getManagerByUI();
		ZhongYiWenJuan wenJuan = new ZhongYiWenJuan();
		// wenJuan.setEmpiId(empiId);
		// wenJuan.setPhrId(phrId);
		wenJuan.setIdCard(BaseActivity.getIdCard());
		wenJuan.setEnergyFull_1(manage.getEnergyFull());
		wenJuan.setEasyWeary_2(manage.getEasyWeary());
		wenJuan.setEasyPant_3(manage.getEasyPant());
		wenJuan.setVoiceWeak_4(manage.getVoiceWeak());
		wenJuan.setMoodiness_5(manage.getMoodiness());
		wenJuan.setNervous_6(manage.getNervous());
		wenJuan.setLoneliness_7(manage.getLoneliness());
		wenJuan.setEasyScare_8(manage.getEasyScare());
		wenJuan.setOverweight_9(manage.getOverweight());
		wenJuan.setEyeDry_10(manage.getEyeDry());
		wenJuan.setFootFearCold_11(manage.getFootFearCold());
		wenJuan.setBackFearCold_12(manage.getBackFearCold());
		wenJuan.setFearCold_13(manage.getFearCold());
		wenJuan.setCold_14(manage.getCold());
		wenJuan.setRhinobyon_15(manage.getRhinobyon());
		wenJuan.setMouthGreasy_16(manage.getMouthGreasy());
		wenJuan.setAllergy_17(manage.getAllergy());
		wenJuan.setSkinUrticaria_18(manage.getSkinUrticaria());
		wenJuan.setSkinBleeding_19(manage.getSkinBleeding());
		wenJuan.setSkinRed_20(manage.getSkinRed());
		wenJuan.setSkinDry_21(manage.getSkinDry());
		wenJuan.setLimbsNumb_22(manage.getLimbsNumb());
		wenJuan.setFaceGreasy_23(manage.getFaceGreasy());
		wenJuan.setFaceDim_24(manage.getFaceDim());
		wenJuan.setSkinEczema_25(manage.getSkinEczema());
		wenJuan.setMouthDry_26(manage.getMouthDry());
		wenJuan.setBitterTaste_27(manage.getBitterTaste());
		wenJuan.setBellyLarge_28(manage.getBellyLarge());
		wenJuan.setFearCool_29(manage.getFearCool());
		wenJuan.setStoolStiction_30(manage.getStoolStiction());
		wenJuan.setStoolDry_31(manage.getStoolDry());
		wenJuan.setFurStodgily_32(manage.getFurStodgily());
		wenJuan.setStasisPurple_33(manage.getStasisPurple());// 问题33
		ArchiveBean ab = BaseActivity.getArchiveBean();
		if (ab != null) {
			wenJuan.setPersonName(ab.getName());
			String sex = ab.getSex();
			if ("女".equals(sex)) {
				sex = "2";
			} else if ("男".equals(sex)) {
				sex = "1";
			} else {
				sex = "";
			}
			wenJuan.setSexCode(sex);
		}
		wenJuan.setBirthday(ab.getBirthday());
		// 生成一个json数据用于上传
		wenJuan.setCreateDate(et_date.getText().toString().trim());
		String uploadStr = XmlTool.lnrzyygl2String(manage);
		wenJuan.setEmpiId(time);
		final boolean b = DBService.add(manage);
		final String uploadTime = manage.getTime();
		System.out.println("中医药的time" + uploadTime + b);
		WebService.addZhongYiWenJuan(context, uploadTime, jsonHandler, wenJuan);
	}

	private static final int what_success_ok = 65;
	private static final int what_failed = 77;
	private static final int what_error = 87;
	Handler jsonHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case what_success_ok:
				ToastTool.showLong(context, "上传成功");
				break;
			case what_failed:
				ToastTool.showLong(context, "上传错误");
				break;
			case what_error:
				ToastTool.showLong(context, "访问出错，请检查网络或配置信息");
				break;
			}
		};
	};

	private boolean isEditTextOK() {
		if (TextUtils.isEmpty(et_date.getText().toString())) {
			ToastTool.showShort(context, "填表日期不能为空！");
			return false;
		}
		return true;
	}

	private String getStr2Upload(String str) {
		switch (str) {
		case "情志调摄":
			return "1";
		case "饮食调养":
			return "2";
		case "起居调摄":
			return "3";
		case "运动保健":
			return "4";
		case "穴位保健":
			return "5";
		case "1":
			return "情志调摄";
		case "2":
			return "饮食调养";
		case "3":
			return "起居调摄";
		case "4":
			return "运动保健";
		case "5":
			return "穴位保健";
		}
		return "";
	}
}
